<!--  -->
<template>
  <div class="app-container">
    <div class="formDiv">
      <el-form ref="form" :inline="true" :model="form" label-width="130px">
        <el-col :span="8">
          <el-form-item label="选择省份">
            <el-select v-model="form.province_id" clearable placeholder="请选择">
              <el-option
                v-for="item in provinceList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item>
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button type="primary" @click="handleReset">重置</el-button>
            <el-button type="primary" @click="toRoutes('addHot','add')">添加</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </div>
    <!-- 主页列表 -->
    <div v-loading="tableLoading" class="listDiv">
      <el-table :data="tableData" class="tableData" style="width: 100%">
        <el-table-column prop="province" label="省份" align="center" />
        <el-table-column label="上架状态" align="center" prop="is_on_shelf">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.is_on_shelf"
              class="switchStyle"
              active-color="#13ce66"
              inactive-color="#eee"
              :active-value="0"
              :inactive-value="1"
              @click.native="changeStatus(scope.row.province_id,scope.row.is_on_shelf)"
            />
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="创建时间" align="center" />
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="toRoutes('addHot',scope.row.province_id,'edit')"
            >编辑</el-button>
            <el-button type="text" size="small" @click="handleDel(scope.row.province_id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagesCon">
        <div>
          <el-pagination
            :current-page="form.page"
            :page-sizes="page_sizes"
            :page-size="form.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page_totals"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { hotIndex, hotOnshelf, hotProvinceDel } from "@/api/appList";
export default {
  data() {
    return {
      form: {
        province_id: "",
        page: 1,
        pageSize: 10,
      },
      provinceList: [],
      tableData: [],
      tableLoading: true,
      page_totals: 0,
      page_sizes: [10, 30, 50, 100],
    };
  },

  mounted() {
    this.getList(this.form);
  },

  methods: {
    toRoutes(name, id, type) {
      const route = { path: name, query: { id: id, type, type } };
      this.$router.push(route);
    },
    async getList(opt) {
      let { data } = await hotIndex(opt);
      // console.log("data", data);
      if (data.status == 0) {
        // console.log(888);
        this.tableLoading = false;
        this.tableData = data.data.list;
        this.provinceList = data.data.provinceArray;
        this.page_totals = data.data.total;
      } else {
        this.tableLoading = false;
        this.$message.error(data.message);
      }
    },
    // 修改上下架状态
    async changeStatus(province_id, on_shelf) {
      let data = { province_id, on_shelf };
      let res = await hotOnshelf(data);
      if (res.data.status == 0) {
        this.$message.success("修改成功");
      } else {
        this.$message.error(res.data.message);
      }
    },
    // 删除
    handleDel(province_id) {
      const data = { province_id };
      this.$confirm("此操作将会删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          hotProvinceDel(data)
            .then((res) => {
              console.log("res", res.data);
              if (res.data.status == 0) {
                this.$message.success("删除成功");
                this.getList(this.form);
              } else {
                this.$message.error(res.data.message);
              }
            })
            .catch((err) => {
              this.$message.error(res.data.message);
            });
        })
        .catch(() => {
          this.$message.info("已取消");
        });
    },
    // 查询
    handleSearch() {
      this.form.page = 1;
      this.getList(this.form);
    },
    //重置
    handleReset() {
      this.form.province_id = "";
      this.getList(this.form);
    },
    // 当前页
    handleSizeChange(val) {
      this.form.pageSize = val;
      this.getList(this.form);
    },
    // 选择页数
    handleCurrentChange(val) {
      this.form.page = val;
      this.getList(this.form);
    },
  },
};
</script>
<style  scoped>
.pagesCon {
  text-align: right;
  margin-top: 20px;
}
</style>